library(leaflet)
library(dplyr)
library(geojsonio)
library(htmltools)
library(RColorBrewer)
1: read file in
nycounties <- geojson_read("../data/nyczip.geojson", what = "sp")
sales <- read.csv("../data/Sale_Data.csv", header=TRUE, stringsAsFactors = FALSE)
nycounties$postalCode <- as.numeric(as.character(nycounties$postalCode))
dat <- nycounties@data
salesPrice <- NULL
for(i in 1:nrow(dat)){
zip <- dat$postalCode[i]
salesPrice <- c(salesPrice, sales$Price_Predict[sales$RegionName == zip])
}
nycounties@data$sales <- salesPrice
rental <- read.csv("../data/Rental_Data.csv", header=TRUE, stringsAsFactors = FALSE)
#for(i in 1:5){
# bed <- filter(rental, Bedroom == i)
# write.csv(bed, paste("~/Desktop/ADS/Fall2017-project2-fall2017-project2-grp1/data/rental_bed", i,".csv", sep = ""))
#}
dat <- nycounties@data
l <- list()
for (i in 1:5){
s<-read.csv(paste("../data/rental_bed",i,".csv", sep=""), header=TRUE, stringsAsFactors = FALSE)
# s$RegionName <- as.character(s$RegionName)
rentalPrice <- NULL
for(j in 1:nrow(dat)){
zip <- dat$postalCode[j]
rentalPrice <- c(rentalPrice, s$Price_Predict[s$RegionName == zip])
}
l[[i]] <- rentalPrice
}
nycounties@data$rental_Bed1 <- l[[1]]
nycounties@data$rental_Bed2 <- l[[2]]
nycounties@data$rental_Bed3 <- l[[3]]
nycounties@data$rental_Bed4 <- l[[4]]
nycounties@data$rental_Bed5 <- l[[5]]
hospital <- read.csv("../data/Hospital_Data.csv", header=TRUE, stringsAsFactors = FALSE)
#hospital$Longitude <- hospital$Longitude*(-1) # correct the longitugde
art <- read.csv("../data/Art_Gallery_Data.csv", header=TRUE, stringsAsFactors = FALSE)
# coord <- sapply(art$the_geom, function(x) {
# substr(x, 8, nchar(x)-1)
# }) %>% as.character()
# values <- strsplit(coord, split=" ") %>% unlist()
# art_long <- NULL
# art_lat <- NULL
# for(i in 1:(length(values)/2)){
# art_long <- c(art_long,values[i*2-1])
# art_lat <- c(art_lat, values[i*2])
# }
# art_long <- art_long %>% as.numeric()
# art_lat <- art_lat %>% as.numeric()
# art$longitude <- art_long
# art$latitude <- art_lat
theatre <- read.csv("../data/Theatre_Data.csv", header=TRUE, stringsAsFactors = FALSE)
# coord <- sapply(theatre$the_geom, function(x) {
# substr(x, 8, nchar(x)-1)
# }) %>% as.character()
# values <- strsplit(coord, split=" ") %>% unlist()
# theatre_long <- NULL
# theatre_lat <- NULL
# for(i in 1:(length(values)/2)){
# theatre_long <- c(theatre_long,values[i*2-1])
# theatre_lat <- c(theatre_lat, values[i*2])
# }
# theatre_long <- theatre_long %>% as.numeric()
# theatre_lat <- theatre_lat %>% as.numeric()
# theatre$longitude <- theatre_long
# theatre$latitude <- theatre_lat
subway <- read.csv("../data/Subway_Data.csv", header=TRUE, stringsAsFactors = FALSE)
crime <- read.csv("../data/Crime_Data.csv", header=TRUE, stringsAsFactors = FALSE)
crime <- crime %>% na.omit()
2 make the base map color = “#444444”
basemap <- leaflet(nycounties) %>% addPolygons( color = "#BFA",weight = 1, smoothFactor = 0.5, opacity = 1.0, fillOpacity = 0.5, highlightOptions = highlightOptions(color = "red", weight = 2, bringToFront = TRUE)) %>% addTiles()
IconMaker <- function(address){
icon <- makeIcon(
iconUrl = address,
iconWidth = 30, iconHeight = 30,
iconAnchorX = 0, iconAnchorY = 0)
}
hospitalIcon <- IconMaker("../fig/Hospital.png")
galleryIcon <- IconMaker("../fig/Gallery.png")
theatreIcon <- IconMaker("../fig/Theatre.png")
subwayIcon <- IconMaker("../fig/Subway.png")
crimeIcon <- IconMaker("../fig/Crime.png")
pops_hospital <- paste0("<center style='color:black'><strong style='color:blue'>",hospital$Facility.Name,"</strong>","<br/>",hospital$Phone, "<br/>", hospital$address, "</center>")
basemap %>% addMarkers(lng = hospital$Longitude, lat = hospital$Latitude, clusterOptions = markerClusterOptions() , popup = pops_hospital, icon=hospitalIcon)
pops_art <- paste0("<center style='color:black'><strong style='color:blue'>",art$NAME,"</strong>","<br/>",paste(art$ADDRESS1, art$ADDRESS2), "</center>")
basemap %>% addMarkers(lng = art$long, lat = art$lat,clusterOptions = markerClusterOptions() , popup = pops_art, icon=galleryIcon)
pops_theatre <- paste0("<center style='color:black'><strong style='color:blue'>",theatre$NAME,"</strong>","<br/>",paste(theatre$ADDRESS1, theatre$ADDRESS2), "</center>")
basemap %>% addMarkers(lng = theatre$long, lat = theatre$lat,clusterOptions = markerClusterOptions() , popup = pops_theatre, icon=theatreIcon)
pops_subway <- paste0("<center style='color:black'><strong style='color:blue'>",subway$NAME,"</strong>","<br/>",subway$LINE, "</center>")
basemap %>% addMarkers(lng = subway$Long, lat = subway$Lat,clusterOptions = markerClusterOptions(), popup = pops_subway, icon=subwayIcon)
pops_crime <- paste0("<center style='color:black'><strong style='color:blue'>",crime$LAW_CAT_CD,"</strong>","<br/>",crime$BORO_NM, "</center>")
basemap %>% addMarkers(lng = crime$Longitude, lat = crime$Latitude,clusterOptions = markerClusterOptions(), popup = pops_crime, icon=crimeIcon)